package com.iacrqq.sedaf.config;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;

public class FileSystemConfiguration implements Configuration {

	Properties properties = new Properties();
	
	@Override
	public void set(String key, Object value)
	{
		properties.put(key, value);
	}

	public FileSystemConfiguration(String configFile)
	{
		try
		{
			properties.load(new FileReader(new File(configFile)));
		}
		catch(FileNotFoundException e)
		{
			throw new RuntimeException(e);
		}
		catch(IOException e)
		{
			throw new RuntimeException(e);
		}
	}
	
	@Override
	public int getInt(String key) {
		String strValue = (String)properties.get(key);
		return Integer.valueOf(strValue.trim());
	}

	@Override
	public int getInt(String key, int defaultValue) {
		String strValue = (String)properties.get(key);
		if(strValue == null)
		{
			return defaultValue;
		}
		
		return Integer.valueOf(strValue.trim());
	}
	
	@Override
	public String getString(String key)
	{
		return (String)properties.getProperty(key);
	}

	@Override
	public String getString(String key, String defaultValue)
	{
		String value = (String)properties.getProperty(key);
		
		if(value == null)
		{
			return defaultValue;
		}
		
		return value;
	}
}
